'use strict';
const path = require('path');
const config = require('./config');
const assetsDir = 'static';
const myTheme = path.resolve(__dirname, "./src/styles/less/vantChange.less");
console.log(process.env.VUE_APP_BASE_API)
// 方便发布时同时编译不同环境
const distDir = (function getDistDir () {
  const distDirs = ['stg', 'test'];
  let r = 'dist';
  const lastArgv = process.argv[process.argv.length - 1];
  let i = 0;
  if (lastArgv && (i = distDirs.indexOf(lastArgv)) > -1) {
    r = r + '_' + distDirs[i];
  }
  return r;
})();

function resolve (dir) {
  return path.join(__dirname, dir);
}
const lastArgv = process.argv[process.argv.length - 1];
if (lastArgv === '--xwy') {
  process.env.is_xwy = '--xwy'
}
var configureWebpack = {
  // name:process.env.VUE_APP_ID,
  optimization: {
    runtimeChunk: {
      name: assetsDir + '/js/manifest'
    },
    minimize: true,
    splitChunks: {
      chunks: 'all',
      name: true,
      maxSize: 512 * 1024,
      minSize: 20 * 1024,
      cacheGroups: {
        // 处理入口chunk
        vendors: {
          name: 'vendors',
          test: /[\\/]node_modules[\\/]/,
          chunks: 'initial',
          priority: 10
        },
        // 处理异步chunk
        'async-vendors': {
          name: 'async-vendors',
          test: /[\\/]node_modules[\\/]/,
          chunks: 'async',
          priority: 15
        },
        components: {
          name: 'components.dll',
          priority: 25,
          test: /[\\/]src[\\/]components.*[\\/]/
        }
      }
    }
  }
};

if (process.env.NODE_ENV != 'development') {
  configureWebpack.output = {
    filename: '[name].[contenthash:7].js'
  };
}
// console.log(process.env)
module.exports = {
  outputDir: distDir,
  publicPath: process.env.NODE_ENV === 'production' ? '/' : '/',
  assetsDir: assetsDir,
  filenameHashing: true,
  productionSourceMap: process.env.NODE_ENV === 'development',
  devServer: {
    host: config.Host,
    port: config.Port,
    proxy: {
      [process.env.VUE_APP_BASE_API]: {
        target: `http://213.183.48.136:8746/`,
        changeOrigin: true,
        pathRewrite: {
          ['^' + process.env.VUE_APP_BASE_API]: ''
        }
      }
    },
    disableHostCheck: true
  },
  css: {
    loaderOptions: {
      sass: {
        alias: {
          '@': resolve('./src')
        },
        prependData: `@import "~@/styles/index.scss"`
      },
      less: {
        // 若 less-loader 版本小于 6.0，请移除 lessOptions 这一级，直接配置选项。
        // lessOptions: {
        modifyVars: {
          // 'blue': '#009588',
          // 'green': '#07c160',
          // 'text-color': 'red'
          hack: `true; @import "${myTheme}";`
        // }
        },
      },
      scss: {
        prependData: `@import "~@/styles/index.scss";`
      }
    }
  },
  transpileDependencies: [],

  chainWebpack: config => {
    config.plugins.delete('preload'); // TODO: need test
    config.plugins.delete('prefetch'); // TODO: need test
    // set svg-sprite-loader

    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end();
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end();

    config.resolve.alias.set('@', resolve('./src'));
    // config
    //   .module
    //   .rule("images")
    //   .test(/\.(jpg|png|gif)$/)
    //   .use("url-loader")
    //   .loader("url-loader")
    //   .options({
    //     publicPath: '/' + assetsDir + '/images',
    //     outputPath: assetsDir + '/images',
    //     name: '[name]_[hash:7].[ext]',
    //     limit: 1024
    //   })
    //   .end();
  },

  configureWebpack
};
